package com.jiinfo.taglib.dic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.jfinal.kit.JsonKit;
import com.jiinfo.config.ConstConfig;
import com.jiinfo.model.DicNode;

public class GetJsonTag extends TagSupport{

	private static final long serialVersionUID = 6916910773256433677L;
	private static Map<String,String> map=new HashMap<String,String>();
	
	private String path;
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}


	@SuppressWarnings("static-access")
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();// 得到out
		try {
			if(path!=null&&!"".equals(path)){
				if(!map.containsKey(path)){
					String[] paths=path.split(ConstConfig.DIC_DIVISION);
					DicNode dicNode=DicNode.dao.findFirst("select * from t_dic_node t where t.parentId is null and t.code=?",paths[0]);
					for (int i=1;i<paths.length;i++) {
						List<DicNode> dicNodes=dicNode.getDicNodes();
						for (DicNode dicNode2 : dicNodes) {
							if(paths[i].equals(dicNode2.get("code"))){
								dicNode=dicNode2;
								break;
							}else{
								dicNode=null;
							}
						}
					}
					
					map.put(path, JsonKit.toJson(dicNode.getDicNodes()));
				}
					
				out.print(map.get(path));
			}else{
				out.print("");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.EVAL_PAGE;
	}
	
}
